require('../model/merchantSchema');
const _ = require('lodash');
const uuid = require('uuid');
var mongoose = require('mongoose');
var merchantModel = mongoose.model('Merchant');

module.exports = {
    addMerchant:addMerchant,
    findMerchantByStatus:findMerchantByStatus,
    findMerchantByBan:findMerchantByBan,
    updateStatusById:updateStatusById,
    updateBanById:updateBanById,
    updateMerchent:updateMerchent,
    findAll:findAll,
    findMerchantById:findMerchantById,
    findStatusById:findStatusById
}

/**
 * find status by id
 * @param id
 * @param callback
 */
function findStatusById(id,callback){
    merchantModel.findOne({_id:id},{status:1,rejectContent:1}).exec(function(err,doc){
        callback(err,doc);
    });
}

/**
 * find merchant by id
 * @param id
 * @param callback
 */
function findMerchantById(id,callback){
    merchantModel.findOne({_id:id}).exec(function(err,doc){
        callback(err,doc);
    });
}

/**
 * find all merchants
 */
function findAll(callback){
    merchantModel.find({}).exec(function(err,docs){
        callback(err,docs);
    })
}

/**
 * update merchant data by id
 * @param merchant
 * @param callback
 */
function updateMerchent(merchant,callback){
    merchantModel.updateMany({_id:merchant._id},{
        mName:merchant.mName,
        shopName:merchant.shopName,
        address:merchant.address,
        imgHead:merchant.imgHead,
        imgCard:merchant.imgCard,
        ban:merchant.ban,
        rejectContent:merchant.rejectContent,
        status:merchant.status,
        createTime:merchant.createTime
    },function(err,result){
        callback(err,result);
    });
}

/**
 * update merchant ban status by id
 * @param ban
 * @param id
 * @param callback
 */
function updateBanById(ban,id,callback){
    merchantModel.updateMany({_id:id},{
        ban:ban
    },function(err,doc){
        callback(err,doc);
    });
}

/**
 * update merchant status by id
 * @param status
 * @param id
 * @param callback
 */
function updateStatusById(status,id,callback){
    merchantModel.updateMany({_id:id},{
        status:status
    },function(err,doc){
        callback(err,doc);
    });
}

/**
 * find merchant by ban
 * @param ban
 * @param callback
 */
function findMerchantByBan(ban,callback){
    merchantModel.find({ban:ban}).exec(function(err,docs){
        callback(err,docs);
    });
}

/**
 * find merchant by status
 * @param status
 * @param callback
 */
function findMerchantByStatus(status,callback){
    merchantModel.find({status:status}).sort({createTime:1}).exec(function(err,docs){
        callback(err,docs);
    });
}

/**
 * add merchant
 * @param merchant
 * @param callback
 */
function addMerchant(merchant,callback){
    merchantModel.create(merchant,function (err,doc) {
        callback(err,doc);
    });
}



